<template>
  <div>
    <p class="pinformation">搜索条件</p>
    <el-form :model="queryParams" label-width="85px" ref="queryRef">
      <el-row>
        <el-col :span="5">
          <el-form-item label="检测类别：" prop="jclbId">
            <el-select filterable clearable v-model="queryParams.jclbId" @change="handleQuery">
              <el-option v-for="item in jclbTypeList" :key="item.jclbId" :value="item.jclbId"
                :label="item.jclbName"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="点位名称：" prop="pointName">
            <el-input v-model="queryParams.pointName" placeholder="请输入点位名称" @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="周期类型：" prop="cycleType">
            <el-select v-model="queryParams.cycleType" filterable @change="handleQuery">
              <el-option v-for="dict in sys_point_cycle" :key="dict.value" :label="dict.label"
                :value="parseInt(dict.value)" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-button type="success" style="margin-left: 10px" @click="handleQuery">搜索</el-button>
          <el-button class="business resetBtn" @click="resetQuery">重置</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <p class="pinformation">点位列表</p>
      <p class="totalStyle">共{{ total }}条数据</p>
      <el-row style="padding-bottom: 5px; margin-left: 10px;">
        <el-button :disabled="props.isDisabled" link type="success" icon="Plus" @click="handleAddPoint"
          v-show="status == 0">新增</el-button>
        <el-button :disabled="props.isDisabled" link type="success" icon="Refresh" @click="handlePasteCustom"
          v-show="status == 0">源自客户信息库</el-button>
        <el-button :disabled="props.isDisabled" link type="success" icon="Refresh"
          @click="handleRenewCustom">更新客户信息库</el-button>
          <el-button   link type="success" icon="View" 
          @click="handleJcnr">检测内容</el-button>
      </el-row>
    </el-row>
    <el-table v-loading="pointTableLoading" :data="pointList">
      <el-table-column label="点位名称" align="center" prop="pointName" />
      <el-table-column label="检测类别" align="center" prop="jclbId">
        <template #default="scope">
          {{ getJclbName(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column label="连测天数" align="center" prop="sampleDay" />
      <el-table-column label="每天频次" align="center" prop="sampleCount" />
      <el-table-column label="周期" align="center" width="70px">
        <template #default="scope">
          <dict-tag :options="sys_point_cycle" :value="scope.row.cycleType" />
        </template>
      </el-table-column>
      <el-table-column label="周期次数" align="center" prop="cycleCount" />
      <el-table-column label="检测项目数" align="center" prop="jcxmCount" />
      <el-table-column label="分包项目数" align="center" prop="jcxmPCount" />
      <el-table-column label="操作" align="center" width="210px">
        <template #default="scope">
          <el-button v-if="status == 1" link type="success" icon="EditPen"
            @click="handleViewPoint(scope.row)">查看</el-button>
          <el-button v-if="status == 0" link type="success" icon="EditPen"
            @click="handleEditPoint(scope.row)">编辑</el-button>
          <el-button v-if="status == 0" link type="success" icon="CopyDocument"
            @click="handleCopyPoint(scope.row)">复制</el-button>
          <el-button v-if="status == 0" link type="success" icon="Delete"
            @click="handlePointDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="queryPointList" />
    <PointLocationDialog ref="PTDialogRef" :pointId="pointId" :queryPointList="queryPointList" :primaryKeyId="props.zjId"
      @destroyPointDialog="destroyPointDialog" v-if="pointDialogDestroy" />
    <viewDialog ref="viewDialogRef" :pointId="pointId" />
    <jcnrDialog ref="jcnrDialogRef" :primaryId="props.zjId" v-if="showJcnr"/>
  </div>
</template>

<script setup>
import { pasteCustomPoint, RenewCustomPoint } from "@/api/contract/check";
import { allListJclb } from "@/api/store/jclb";
import { listPoint, delPoint, copyPoint } from "@/api/contract/taskPoint"
import PointLocationDialog from "../point/index.vue"
import jcnrDialog from "../point/jcnr.vue"
import viewDialog from "./viewDialog"
const { proxy } = getCurrentInstance();
const { sys_point_cycle } = proxy.useDict("sys_point_cycle");
const jclbTypeList = ref([]);
const total = ref(0);
const viewDialogRef = ref(null)
const pointList = ref([]) // 点位列表
const pointTableLoading = ref(false) // 点位表格加载
// const jclbTypeList = ref([]); // 检测类别 list
const pointId = ref();
const pointDialogDestroy = ref(false);
const showJcnr = ref(false);
const data = reactive({
  queryParams: {
    pageSize: 10,
    pageNum: 1
  },
});
const { queryParams } = toRefs(data);
const props = defineProps({
  zjId: {
    type: String,
    default: null
  },
  status: {
    type: Number
  },
  isDisabled: {
    type: Boolean,
    default: true
  }
});
const status = ref(props.status);
watch(() => props.status, (newVal) => {
  status.value = newVal;
});

function handleQuery() {
  queryPointList();
};
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.primaryId = props.zjId;
  handleQuery();
};
function assignmentId() {
  nextTick(function () {
    queryParams.value.primaryId = props.zjId;
    queryPointList();
  });
  getJclbTypeList();
}

// 获取点位列表数据
function queryPointList() {
  pointTableLoading.value = true // 开启加载
  // 根据客户id查询点位列表
  listPoint(queryParams.value).then(res => {
    pointList.value = res.rows // 保存点位列表
    total.value = res.total
    pointTableLoading.value = false // 关闭加载
  });
}

//打开点位弹出窗口
function handleAddPoint() {
  pointDialogDestroy.value = true;
  nextTick(() => {
    proxy.$refs["PTDialogRef"].form.primaryId = queryParams.value.primaryId
    // 打开新增/编辑检测方法dialog
    proxy.$refs["PTDialogRef"].isShowPTDialog = true
  });
}

function handleJcnr(){
  showJcnr.value = true;
  nextTick(() => {
    proxy.$refs["jcnrDialogRef"].getForm(queryParams.value.primaryId);
    proxy.$refs["jcnrDialogRef"].isShow = true
  });
}

function handlePasteCustom() {
  if (pointList.value.length > 0) {
    proxy.$modal
      .confirm("当前点位列表已有信息，导入客户信息库污染源信息将覆盖当前列表所有信息，确定导入吗？")
      .then(async () => {
        handleCustomPoint();
      })
      .catch(() => { });
  }
  else {
    handleCustomPoint();
  }
}

function handleCustomPoint() {
  pasteCustomPoint(queryParams.value.primaryId).then(res => {
    proxy.$modal.msgSuccess("导入成功");
    queryPointList();
  });
}

function handleRenewCustom() {
  if (pointList.value.length > 0) {
    proxy.$modal
      .confirm("更新点位列表已有信息，确定更新吗？")
      .then(async () => {
        handleRenewPoint();
      })
      .catch(() => { });
  }
  else {
    handleRenewPoint();
  }
}

function handleRenewPoint() {
  RenewCustomPoint(queryParams.value.primaryId).then(res => {
    proxy.$modal.msgSuccess("更新成功");
    queryPointList();
  });
}

// 污染源信息 => 点位列表 => 点击表格编辑
function handleEditPoint(row) {
  pointDialogDestroy.value = true;
  nextTick(() => {
    proxy.$refs["PTDialogRef"].isShowPTDialog = true
    pointId.value = row.pointId;
  });
}

function handleCopyPoint(row) {
  if(row.jcxmCount == 0){
    proxy.$modal.msgError("复制失败，当前点位还未添加检测项目！");
    return;
  }
  pointDialogDestroy.value = true;
  copyPoint(row.pointId).then(res => {
    nextTick(() => {
      proxy.$refs["PTDialogRef"].isCopy = true;
      proxy.$refs["PTDialogRef"].isShowPTDialog = true
      pointId.value = res.data;
    });
  });
}
function handlePointDel(row) {
  proxy.$modal
    .confirm("删除该点位会将该点位下的点位信息及关联的检测项目信息全部删除，确定要删除吗？")
    .then(async () => {
      await delPoint(row.pointId) // 根据点位id删除
      proxy.$modal.msgSuccess("删除成功")
      queryPointList();
    })
    .catch(() => { });
}

function handleViewPoint(row) {
  pointId.value = row.pointId;
  viewDialogRef.value.showDialog()
}
// 比对检测类别
function getJclbName(row) {
  return proxy.getItemByArray(jclbTypeList.value, "jclbId", row.jclbId, 'jclbName');
}

// 获取检测类别数据
function getJclbTypeList() {
  allListJclb({ typeId: 1 }).then(res => {
    jclbTypeList.value = res.data;
  });
}

function destroyPointDialog() {
  pointDialogDestroy.value = false;
  pointId.value = null;
}

defineExpose({
  assignmentId
});
</script>

<style></style>